<? 
require("adm_header.php"); 
ini_set("max_execution_time","1000");
?>

<? writeCommonAdminTable(LA_ADMIN_NEWSL_TITLE,1); ?>

<?
if (getParam("preview",""))
{
	
	echo "<p class='bg'>".LA_ADMIN_NEWSL_NOTSENTYET."</p>";
	msgSend();	
	ShowForm("");
}
elseif (getParam("submit",""))
{
	echo "<p></p>";
	msgSend();	
}
else 
{
	echo "<p class='bg'>" . LA_ADMIN_NEWSL_DESCR . "</p>";
	
	echo "<p>".LA_ADMIN_NEWSL_AVAILABLE.": %name%,%mail%,%num_ads%,%score%,%user_id%</p>";
	ShowForm(1);
}

function msgSend()
{
	global $usr_tbl,$set_webmaster_address,$set_demo;
    $lGetTitle=getParam("title",""); 
	$lGetMessage=getParam("message",""); 
    
	
	$sql_email = "select distinct user_name,user_email,user_num_ads,user_date_last_login,user_score,user_id from $usr_tbl where user_newsletter = 'on' AND user_email <> ''";
	 if (getParam("preview","")!="")
  		$sql_email.=" limit 3";
	$result = q ($sql_email);
    $num_email =  mysql_num_rows($result);
	
     while ($row = mysql_fetch_array($result))
     {
     		$count++;
     	$lName = $row["user_name"];
     	$lEmail = $row["user_email"];
     	$lNumAds = $row["user_num_ads"];
     	$lLastLogin = formatDateShort($row["user_date_last_login"]);
		$lUserScore = $row["userscore"];
     	$lUserID = $row["user_id"];
     	
     	$lTitle=$lGetTitle;
     	$lMessage=$lGetMessage;
     	
     	// Title
     	$lTitle=eregi_replace("%name%",$lName,$lTitle);
     	$lTitle=eregi_replace("%mail%",$lEmail,$lTitle);
     	$lTitle=eregi_replace("%last_login%",$lLastLogin,$lTitle);
     	$lTitle=eregi_replace("%num_ads%",$lNumAds,$lTitle);
     	$lTitle=eregi_replace("%score%",$lUserScore,$lTitle);
     	$lTitle=eregi_replace("%user_id%",$lUserID,$lTitle);
     	
     	// Body part
     	$lMessage=eregi_replace("%name%",$lName,$lMessage);
     	$lMessage=eregi_replace("%mail%",$lEmail,$lMessage);
     	$lMessage=eregi_replace("%last_login%",$lLastLogin,$lMessage);
     	$lMessage=eregi_replace("%num_ads%",$lNumAds,$lMessage);
     	$lMessage=eregi_replace("%score%",$lUserScore,$lMessage);
     	$lMessage=eregi_replace("%user_id%",$lUserID,$lMessage);
     	
     	if (getParam("preview","") !="")
     		echo "<pre>$lTitle<br>$lMessage</pre><hr>";
     	else
     	{
     		if (!$set_demo)
     			sendEmail($lEmail,$set_webmaster_address,$lTitle,$lMessage);
     		echo "$count. $lEmail<br>";	
     	}
     
     }
     if (getParam("preview","")=="")
     	echo "<p><b>$count</b> ".LA_ADMIN_NEWSL_NUM_USERS_RECIEVED."</p>";

}

function ShowForm($aPreview)
{
  global $usr_tbl,$set_webmaster_address;
  echo "<form method='post' action='adm_newsletter.php'>";
  echo "<input type='hidden' name='preview' value='$aPreview'>";

  
  $sql_email = "select distinct user_name,user_email from $usr_tbl where user_newsletter = 'on' AND user_email <> ''";
  $result = q ($sql_email);
  $num_email =  mysql_num_rows($result);
  
  echo "<p><b>$num_email</b> ".LA_ADMIN_NEWSL_NUM_USERS_RECIEVE."</p>";

  echo "<p>&nbsp;<p><b>".LA_ADMIN_NEWSL_FORM."</b><p>
	<table border='0' cellspacing='1' width='100%' class='noBorder'>
	<tr>
	 <td width='50%' valign='top'> ".LA_ADMIN_NEWSL_SUBJECT." </td>
	 <td width='50%' valign='top'> <input type='text' name='title' size='42' value='".getParam("title","")."'></td>
	</tr>
	<tr>
	 <td width='50%' valign='top'> ".LA_ADMIN_NEWSL_FROM_ADDRESS." </td>
	 <td width='50%' valign='top'> $set_webmaster_address </td>
	</tr>
	<tr>
	 <td width='50%' valign='top'> ".LA_ADMIN_NEWSL_MESSAGE." </td>
	 <td width='50%' valign='top'><textarea rows='8' name='message' id='message' cols='42'>".getParam("message","")."</textarea></td>
	</tr>
	</table>
	<input type='submit' value='".LA_ADMIN_NEWSL_SEND."' name='submit'>
	
	</form>";

}
?>
 
<? writeCommonAdminTable("Email Users",""); ?><!-- END Table menu -->
<? require("adm_footer.php"); ?>
